<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <div class="leftbox">
      <!-- 树节点过滤 -->
      <div>
        <el-input
          placeholder="输入关键字进行过滤"
          size="small"
          v-model="filterText"
        >
        </el-input>
      </div>
      <!-- 树组件 -->
      <div style="margin-top: 15px">
        <el-tree
          ref="tree"
          :props="props"
          :highlight-current="true"
          @node-click="handleNodeClick"
          :filter-node-method="filterNode"
          :load="loadNode"
          lazy
        >
        </el-tree>
      </div>
    </div>
    <!-- 盒子主体 -->
    <div class="bodybox">
      <!-- 新增按钮 -->
      <div class="top">
        <div class="title">短信模板配置</div>
        <button class="buttom" @click="submit('add')">
          <i class="el-icon-plus" style="font-size: 12px; color: #fff"></i>新增
        </button>
      </div>
      <!-- 循环图片盒子 -->
      <div class="formbox">
        <div class="littlebox" v-for="item in entity" :key="item.id">
          <span class="title" :style="{ fontSize: '18px' }">{{
            item.orgName || item.manageName
          }}</span>
          <div class="boxbtn">
            <el-button type="text" style="color: #4095e5" @click="edit(item)"
              ><i class="el-icon-edit"></i
            ></el-button>
            <el-button type="text" style="color: red" @click="del(item.id)"
              ><i class="el-icon-delete"></i
            ></el-button>
          </div>
          <div class="contain">
            <el-row>
              <el-col :span="8">模板名称:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{ item.name }}</span></el-col
              >
            </el-row>
            <el-row>
              <el-col :span="8">短信类型:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  smsselect.find((i) => i.value === item.smsType)?.text
                }}</span></el-col
              >
            </el-row>
            <el-row :style="{ height: '42px' }">
              <el-col :span="8">APIkey:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  item.apiKey
                }}</span></el-col
              >
            </el-row>
            <el-row>
              <el-col :span="8">模板编号:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb', 'word-wrap': 'break-word' }">{{
                  item.identifier
                }}</span></el-col
              >
            </el-row>
            <el-row>
              <el-col :span="8">短信模板:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{
                  item.content
                }}</span></el-col
              >
            </el-row>

            <el-row>
              <el-col :span="8">维护时间:</el-col>
              <el-col :span="16"
                ><span :style="{ color: '#bbb' }">{{
                  item.createTime
                }}</span></el-col
              >
            </el-row>
          </div>
        </div>
      </div>
      <!-- 新增/修改弹框 -->
      <div>
        <el-dialog
          :show-close="false"
          :visible.sync="dialogVisible"
          width="500px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :center="true"
        >
          <div class="dialog">
            <div class="title">短信模板配置信息</div>
            <!-- 企业下拉 -->
            <el-form
              ref="dialogForm"
              :rules="rules"
              :model="dialogForm"
              label-width="110px"
            >
              <el-form-item label="模板名称:" prop="name">
                <el-input
                  v-model="dialogForm.name"
                  placeholder="请输入模板名称"
                ></el-input>
              </el-form-item>
              <el-form-item label="维护组织" prop="orgName">
                <el-select
                  disabled
                  v-model="dialogForm.orgName"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in options"
                    :key="index"
                    :label="item.orgName"
                    :value="item.orgNo"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="短信类型:" prop="smsType">
                <el-select
                  v-model="dialogForm.smsType"
                  placeholder="请选择短信类型"
                >
                  <el-option
                    v-for="item in smsselect"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="APIkey:" prop="apiKey">
                <el-input
                  v-model="dialogForm.apiKey"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="模板编号:" prop="identifier">
                <el-input
                  v-model="dialogForm.identifier"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="短信模板:" prop="content">
                <el-input
                  type="textarea"
                  resize="none"
                  v-model="dialogForm.content"
                  placeholder="请输入短信模板内容"
                ></el-input>
              </el-form-item>
              <el-form-item label="模板短信状态：" props="status">
                <el-switch
                  v-model="dialogForm.status"
                  active-color="#3b8eea"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-form>
            <!-- 弹窗内容 -->
          </div>
          <div style="margin-top: 10px; text-align: center">
            <button
              @click="quxiao"
              style="
                width: 60px;
                height: 30px;
                margin-right: 10px;
                background-color: #fff;
                border: 1px solid #ccc;
                margin-top: 20px;
              "
            >
              取 消
            </button>
            <button
              @click="sure"
              style="
                width: 60px;
                height: 30px;
                background-color: #3b8eea;
                color: #fff;
                border: none;
                margin-top: 20px;
              "
            >
              确 定
            </button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { gettreelist, gettreexiaji } from '../../api/userscreen'
import {
  delSMSListApi,
  getSMSListApi,
  getSMSSelectListApi,
  saveSMSListApi,
  updataSMSListApi
} from '@/api/SMStemplate'
export default {
  name: 'SMStemplateIndex',
  data () {
    return {
      value: '',
      entity: [],
      options: [],
      filterText: '',
      dialogForm: {},
      smsselect: [],
      rules: {
        name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
        smsType: [
          { required: true, message: '请选择短信类型', trigger: 'change' }
        ],
        apiKey: [{ required: true, message: '请输入APIkey', trigger: 'blur' }],
        identifier: [
          { required: true, message: '请输入模板编号', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入短信模板内容', trigger: 'blur' }
        ]
      },
      props: {
        label: 'bizOrgName',
        disabled: 'disabled'
      },
      dialogVisible: false,
      isedit: 'add'
    }
  },
  created () {
    this.getSMSList()
    this.getSMSSelectList()
  },
  async mounted () {},

  methods: {
    // 新增/修改
    submit (val) {
      this.isedit = val
      if (!this.dialogForm.orgName) {
        return this.$message.warning('请先选择企业')
      }
      this.dialogVisible = true
    },
    //  取消/弹框关闭
    quxiao () {
      this.dialogVisible = false
      if (this.isedit === 'add') {
        this.$refs.dialogForm.resetFields()
      } else {
        this.dialogForm = {
          orgName: this.orgName,
          orgNo: this.value
        }
      }
    },
    // 确认修改并保存
    async sure () {
      await this.$refs.dialogForm.validate()
      // if (this.dialogForm.id) {
      //   const res = await updataSMSListApi(this.dialogForm)
      // } else {
      //   const res = await saveSMSListApi(this.dialogForm)
      // }
      const res = this.dialogForm.id
        ? await updataSMSListApi(this.dialogForm)
        : await saveSMSListApi(this.dialogForm)
      if (res.code === 200) {
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.$refs.dialogForm.resetFields()
        await this.getSMSList()
      } else {
        this.$message.success('保存失败')
      }
      console.log(res)
    },
    // 删除头像
    // 树节点点击
    async handleNodeClick (val) {
      console.log(val)
      this.dialogForm.orgName = val.bizOrgName
      this.dialogForm.orgNo = val.bizOrgNo
      this.value = val.bizOrgNo
      this.orgName = val.bizOrgName
      this.getSMSList()
    },
    // 树组件懒加载
    async loadNode (node, resolve) {
      const res = await gettreelist()
      if (node.level === 0) {
        // 初始的级数（最顶层）
        return resolve(res?.data.list) // 最顶层数据渲染为region
      }
      const ress = await gettreexiaji({ orgNo: node.data.bizOrgNo })
      if (node.level === 1) {
        return resolve(ress?.data.list)
      } else if (node.level === 2) {
        return resolve([])
      }
    },
    // 树节点过滤
    filterNode (value, data) {
      if (!value) return true
      return data.bizOrgName.indexOf(value) !== -1
    },
    async getSMSList () {
      const res = await getSMSListApi({
        orgNo: this.value || '',
        pageNo: 0,
        pageSize: 10
      })
      this.entity = res?.data.list
      console.log(res)
    },
    // 获取短信类型下拉框数据
    async getSMSSelectList () {
      const res = await getSMSSelectListApi()
      this.smsselect = res?.data.list
      console.log(this.smsselect)
    },
    // 编辑
    edit (item) {
      this.isedit = 'edit'
      console.log(item)
      this.dialogForm = { ...item }
      this.dialogVisible = true
    },
    // 删除
    async del (id) {
      this.$confirm('此操作将永久删除该档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await delSMSListApi(id)
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getSMSList()
            } else {
              this.$message.success('删除失败')
            }
          } catch (e) {
            console.log(e)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      this.getSMSList()
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.bigbox {
  background-color: #f1f1f1;
  min-height: 100%;
  padding: 10px;
  display: flex;
  .leftbox {
    width: 300px;
    background-color: #fff;
    padding: 20px;
  }
  .bodybox {
    flex: 1;
    margin-left: 10px;
    background-color: #fff;
    padding: 20px;
    .top {
      display: flex;
      justify-content: space-between;
      text-align: right;
      padding-bottom: 10px;
      margin-bottom: 20px;
      border-bottom: 1px solid #bbbbbb;
      .buttom {
        width: 70px;
        height: 30px;
        background-color: #1890ff;
        color: #fff;
        border-radius: 3px;
        border: none;
      }
    }
    .formbox {
      margin-top: 10px;
      display: flex;
      flex-wrap: wrap;
      .littlebox {
        position: relative;
        margin-right: 5%;
        margin-bottom: 20px;
        width: 28%;
        border: 1px solid #1890ff;
        border-radius: 3px;
        padding: 10px;
        .boxbtn {
          position: absolute;
          top: 10px;
          right: 20px;
        }
        .contain {
          margin-top: 40px;
          padding-left: 21px;
          .el-row {
            margin-bottom: 30px;
          }
        }
      }
    }
    .dialog {
      .title {
        font-size: 28px;
        margin-bottom: 30px;
      }
      .text {
        font-size: 12px;
        color: #3b8eea;
      }
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
::v-deep .el-input {
  height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 32px;
}
.el-select-dropdown__item {
  padding-left: 10px;
}
::v-deep .el-date-editor {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>
